package com.gamota.mobi.model;

import java.util.List;
import java.util.Locale;

import main.java.com.u1aryz.android.lib.newpopupmenu.PopupMenu.OnItemSelectedListener;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.RatingBar;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.gamota.mobi.R;
import com.gamota.mobi.adapter.AggregateListAdapter.RowType;
import com.gamota.mobi.adapter.Item;
import com.gamota.mobi.model.ContentItemInfo.RelatedItem;
import com.gamota.mobi.model.ContentItemInfo.Version;
import com.gamota.mobi.util.Utils;

public class SearchItem implements Item {
	
	public int getHasPromotion() {
		return hasPromotion;
	}

	public void setHasPromotion(int hasPromotion) {
		this.hasPromotion = hasPromotion;
	}

	public int getInstallAttachApplication() {
		return installAttachApplication;
	}

	public void setInstallAttachApplication(int installAttachApplication) {
		this.installAttachApplication = installAttachApplication;
	}

	public String getAttachApplicationSlug() {
		return attachApplicationSlug;
	}

	public void setAttachApplicationSlug(String attachApplicationSlug) {
		this.attachApplicationSlug = attachApplicationSlug;
	}

	public String getAttachApplicationTitle() {
		return attachApplicationTitle;
	}

	public void setAttachApplicationTitle(String attachApplicationTitle) {
		this.attachApplicationTitle = attachApplicationTitle;
	}

	public String getAttachApplicationAvatar() {
		return attachApplicationAvatar;
	}

	public void setAttachApplicationAvatar(String attachApplicationAvatar) {
		this.attachApplicationAvatar = attachApplicationAvatar;
	}

	public int getGiftbox() {
		return giftbox;
	}

	public void setGiftbox(int giftbox) {
		this.giftbox = giftbox;
	}

	public int getDaysToGet() {
		return daysToGet;
	}

	public void setDaysToGet(int daysToGet) {
		this.daysToGet = daysToGet;
	}

	public void setOnPopupMenuClick(OnPopupMenuClick onPopupMenuClick) {
		this.onPopupMenuClick = onPopupMenuClick;
	}

	public String getStore() {
		return store;
	}

	public void setStore(String store) {
		this.store = store;
	}

	public String getApplicationId() {
		return applicationId;
	}

	public void setApplicationId(String applicationId) {
		this.applicationId = applicationId;
	}

	public String getIdentifierBundle() {
		return identifierBundle;
	}

	public void setIdentifierBundle(String identifierBundle) {
		this.identifierBundle = identifierBundle;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public int getTotalRate() {
		return totalRate;
	}

	public void setTotalRate(int totalRate) {
		this.totalRate = totalRate;
	}

	public int getSumRate() {
		return sumRate;
	}

	public void setSumRate(int sumRate) {
		this.sumRate = sumRate;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public int getBought() {
		return bought;
	}

	public void setBought(int bought) {
		this.bought = bought;
	}

	public int getIsNew() {
		return isNew;
	}

	public void setIsNew(int isNew) {
		this.isNew = isNew;
	}

	public int getIsHot() {
		return isHot;
	}

	public void setIsHot(int isHot) {
		this.isHot = isHot;
	}

	public int getIsUpdate() {
		return isUpdate;
	}

	public void setIsUpdate(int isUpdate) {
		this.isUpdate = isUpdate;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getTags() {
		return tags;
	}

	public void setTags(String tags) {
		this.tags = tags;
	}

	public int getTotalComment() {
		return totalComment;
	}

	public void setTotalComment(int totalComment) {
		this.totalComment = totalComment;
	}

	public float getRating() {
		return rating;
	}

	public void setRating(float rating) {
		this.rating = rating;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public int getPrice2() {
		return price2;
	}

	public void setPrice2(int price2) {
		this.price2 = price2;
	}

	public int getTotalView() {
		return totalView;
	}

	public void setTotalView(int totalView) {
		this.totalView = totalView;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public String getApplicationType() {
		return applicationType;
	}

	public void setApplicationType(String applicationType) {
		this.applicationType = applicationType;
	}

	public List<String> getDevices() {
		return devices;
	}

	public void setDevices(List<String> devices) {
		this.devices = devices;
	}

	public List<String> getImages() {
		return images;
	}

	public void setImages(List<String> images) {
		this.images = images;
	}

	public String getVideoLink() {
		return videoLink;
	}

	public void setVideoLink(String videoLink) {
		this.videoLink = videoLink;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getChangeLog() {
		return changeLog;
	}

	public void setChangeLog(String changeLog) {
		this.changeLog = changeLog;
	}

	public int getVersionId() {
		return versionId;
	}

	public void setVersionId(int versionId) {
		this.versionId = versionId;
	}

	public String getPublished() {
		return published;
	}

	public void setPublished(String published) {
		this.published = published;
	}

	public int getUpdateFree() {
		return updateFree;
	}

	public void setUpdateFree(int updateFree) {
		this.updateFree = updateFree;
	}

	public double getSize() {
		return size;
	}

	public void setSize(double size) {
		this.size = size;
	}

	public List<RelatedItem> getRelatedApplications() {
		return relatedApplications;
	}

	public void setRelatedApplications(List<RelatedItem> relatedApplications) {
		this.relatedApplications = relatedApplications;
	}

	public List<Version> getVersions() {
		return versions;
	}

	public void setVersions(List<Version> versions) {
		this.versions = versions;
	}

	public long getAppstorevnId() {
		return appstorevnId;
	}

	public void setAppstorevnId(long appstorevnId) {
		this.appstorevnId = appstorevnId;
	}

	public String getPermission() {
		return permission;
	}

	public void setPermission(String permission) {
		this.permission = permission;
	}

	public String getRootLink() {
		return rootLink;
	}

	public void setRootLink(String rootLink) {
		this.rootLink = rootLink;
	}

	public boolean isInstalled() {
		return isInstalled;
	}

	public void setInstalled(boolean isInstalled) {
		this.isInstalled = isInstalled;
	}

	private String applicationId;
	private String identifierBundle;
	private String title;
	private int totalRate;
	private int sumRate;
	private String description;
	private String avatar;
	private int bought;
	private int isNew;
	private int isHot;
	private int isUpdate;
	private String author;
	private String tags;
	private int totalComment;
	private float rating;
	private int price;
	private int price2;
	private int totalView;
	private String time;
	private String applicationType;
	private List<String> devices;
	private List<String> images;
	private String videoLink;
	private String version;
	private String changeLog;
	private int versionId;
	private String published;
	private int updateFree;
	private double size;
	private List<RelatedItem> relatedApplications;
	private List<Version> versions;
	private long appstorevnId;
	private String permission;
	private String rootLink;
	private boolean isInstalled;
	private int giftbox;
	private int daysToGet;
	private String store;
	private Activity context;
	private LayoutInflater li;
	private Typeface robotoMedium;
	private Typeface robotoCondensed;
	private Typeface robotoBoldCondensed;
	private Resources res;
	private int greenColor;
	private int purpleColor;
	private OnPopupMenuClick onPopupMenuClick;
	private int hasPromotion;
	private int installAttachApplication;
	private String attachApplicationSlug;
	private String attachApplicationTitle;
	private String attachApplicationAvatar;
	
	public SearchItem(Activity context){
		this.context = context;
		li = context.getLayoutInflater();
		res = context.getResources();
		robotoBoldCondensed = Typeface.createFromAsset(context.getAssets(), context.getResources().getString(R.string.roboto_bold_condensed));
		robotoCondensed = Typeface.createFromAsset(context.getAssets(), context.getResources().getString(R.string.roboto_condensed));
		robotoMedium = Typeface.createFromAsset(context.getAssets(), context.getResources().getString(R.string.roboto_medium));
		greenColor = context.getResources().getColor(R.color.app_indicator);
		purpleColor = context.getResources().getColor(R.color.gift_indicator);
	}
	
	private AQuery aq;

	@Override
	public int getViewType() {
		// TODO Auto-generated method stub
		return RowType.LIST_ITEM.ordinal();
	}
	
	private int getColor(String store){
		int colorId = R.color.app_indicator;
		if(store.equalsIgnoreCase("apps") || store.equalsIgnoreCase("games")){
			colorId = R.color.app_indicator;
		} else if(store.equalsIgnoreCase("books")){
			colorId = R.color.ebook_indicator;
		} else if(store.equalsIgnoreCase("comics")){
			colorId = R.color.comic_indicator;
		} else if(store.equalsIgnoreCase("movies")){
			colorId = R.color.film_indicator;
		}
		return context.getResources().getColor(colorId);
	}

	@SuppressLint("NewApi")
	@Override
	public View getView(LayoutInflater inflater, View convertView) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = li.inflate(R.layout.short_content_item_search, null);
			holder = new ViewHolder();
			holder.icon = (ImageView) convertView.findViewById(R.id.icon);
			holder.appName = (TextView) convertView.findViewById(R.id.name);
			holder.author = (TextView) convertView.findViewById(R.id.author);
			holder.rating = (RatingBar) convertView.findViewById(R.id.rating);
			holder.price = (TextView) convertView.findViewById(R.id.price);
			holder.priceUnit = (ImageView) convertView.findViewById(R.id.price_unit);
			holder.btnQuickMenu = (ImageButton) convertView.findViewById(R.id.btn_quick_menu);
			holder.ribbonGift = (ImageView) convertView.findViewById(R.id.ribbon_gift);
//			TextView installBtn=(TextView)convertView.findViewById(R.id.installBtn);
//			holder.commentNum=(TextView)convertView.findViewById(R.id.commentNumber);
//			ImageButton btn_quick_menu=(ImageButton)convertView.findViewById(R.id.btn_quick_menu);
//			btn_quick_menu.setVisibility(View.GONE);
//			installBtn.setVisibility(View.GONE);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
			aq = new AQuery(convertView);
			if(giftbox == 1){
				aq.id(holder.ribbonGift).visible();
			} else {
				aq.id(holder.ribbonGift).gone();
			}
			float ratio;
			if(store.equalsIgnoreCase("books") || store.equalsIgnoreCase("comics")){
				ratio = 1.0f / 1.0f;
			} else {
				ratio = AQuery.RATIO_PRESERVE;
			}
//			aq.id(holder.commentNum).text(totalComment+"");
			aq.id(holder.icon).image(avatar, true, true, 0, 0, null, AQuery.FADE_IN_NETWORK, ratio);
			aq.id(holder.appName).text(title).typeface(robotoBoldCondensed);
			aq.id(holder.author).text(author).typeface(robotoCondensed);
			aq.id(holder.rating).rating(rating);
			if(applicationType.equalsIgnoreCase("android")){
				if(isInstalled){
					aq.id(holder.price).text(res.getString(R.string.installed).toUpperCase(Locale.US)).typeface(robotoMedium).textColor(getColor(store));
					aq.id(holder.priceUnit).invisible();
						aq.id(holder.btnQuickMenu).visible().clicked(new OnClickListener() {
							@Override
							public void onClick(View v) {
								if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
									PopupMenu menu = new PopupMenu(context, v);
									menu.getMenuInflater().inflate(R.menu.installed_app_menu, menu.getMenu());
									menu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
										@Override
										public boolean onMenuItemClick(MenuItem menuItem) {
											// TODO Auto-generated method stub
											switch (menuItem.getItemId()) {
											case R.id.menu_launch:
												if(onPopupMenuClick != null){
													onPopupMenuClick.onLaunchClicked(identifierBundle);
												}
												break;
											case R.id.menu_uninstall:
												Utils.uninstallAPK(context, identifierBundle);
												break;
											}
											return true;
										}
									});
									menu.show();
								} else {
									main.java.com.u1aryz.android.lib.newpopupmenu.PopupMenu popupMenuCompat = new main.java.com.u1aryz.android.lib.newpopupmenu.PopupMenu(context);
									popupMenuCompat.add(1, R.string.launch);
									popupMenuCompat.add(2, R.string.uninstall);
									popupMenuCompat.setOnItemSelectedListener(new OnItemSelectedListener() {
										@Override
										public void onItemSelected(main.java.com.u1aryz.android.lib.newpopupmenu.MenuItem menuItem) {
											switch (menuItem.getItemId()) {
											case 1:
												if(onPopupMenuClick != null){
													onPopupMenuClick.onLaunchClicked(identifierBundle);
												}
												break;
											case 2:
												Utils.uninstallAPK(context, identifierBundle);
												break;
											}
										}
									});
									popupMenuCompat.show(v);
								}
							}
						});
				} else {
					if (price == 0 && price2 == 0) {
						aq.id(holder.price).text(res.getString(R.string.free).toUpperCase(Locale.US)).typeface(robotoMedium).textColor(getColor(store));
						aq.id(holder.priceUnit).invisible();
					} else if(price == 0 && price2 != 0){
						aq.id(holder.price).text(String.valueOf(price2)).typeface(robotoMedium).textColor(purpleColor);
						aq.id(holder.priceUnit).image(R.drawable.tym_purple).visible();
					} else if(price != 0 && price2 == 0){
						aq.id(holder.price).text(String.valueOf(price)).typeface(robotoMedium).textColor(greenColor);
						aq.id(holder.priceUnit).image(R.drawable.tym_green).visible();
					}
						aq.id(holder.btnQuickMenu).visible().clicked(new OnClickListener() {
							@Override
							public void onClick(View v) {
								if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
									PopupMenu menu = new PopupMenu(context, v);
									menu.getMenuInflater().inflate(R.menu.not_installed_app_menu, menu.getMenu());
									menu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
										@Override
										public boolean onMenuItemClick(MenuItem menuItem) {
											// TODO Auto-generated method stub
											switch (menuItem.getItemId()) {
											case R.id.menu_install:
												if(onPopupMenuClick != null){
													onPopupMenuClick.onInstallClicked(store, SearchItem.this);
												}
												break;
											case R.id.menu_add_wishlist:
												if(onPopupMenuClick != null){
													onPopupMenuClick.onAddToWishListClicked(store, SearchItem.this);
												}
												break;
											}
											return true;
										}
									});
									menu.show();
								} else {
									main.java.com.u1aryz.android.lib.newpopupmenu.PopupMenu popupMenuCompat = new main.java.com.u1aryz.android.lib.newpopupmenu.PopupMenu(context);
									popupMenuCompat.add(1, R.string.install);
									popupMenuCompat.add(2, R.string.add_to_wishlist);
									popupMenuCompat.setOnItemSelectedListener(new OnItemSelectedListener() {
										@Override
										public void onItemSelected(main.java.com.u1aryz.android.lib.newpopupmenu.MenuItem menuItem) {
											switch (menuItem.getItemId()) {
											case 1:
												if(onPopupMenuClick != null){
													onPopupMenuClick.onInstallClicked(store, SearchItem.this);
												}
												break;
											case 2:
												if(onPopupMenuClick != null){
													onPopupMenuClick.onAddToWishListClicked(store, SearchItem.this);
												}
												break;
											}
										}
									});
									popupMenuCompat.show(v);
								}
							}
						});
				}
			} else if(applicationType.equalsIgnoreCase("ebook") || applicationType.equalsIgnoreCase("comic")){
				if (price == 0 && price2 == 0) {
					aq.id(holder.price).text(res.getString(R.string.free).toUpperCase(Locale.US)).typeface(robotoMedium).textColor(getColor(store));
					aq.id(holder.priceUnit).invisible();
				} else if(price == 0 && price2 != 0){
					aq.id(holder.price).text(String.valueOf(price2)).typeface(robotoMedium).textColor(purpleColor);
					aq.id(holder.priceUnit).image(R.drawable.tym_purple).visible();
				} else if(price != 0 && price2 == 0){
					aq.id(holder.price).text(String.valueOf(price)).typeface(robotoMedium).textColor(greenColor);
					aq.id(holder.priceUnit).image(R.drawable.tym_green).visible();
				}
				aq.id(holder.btnQuickMenu).visible().clicked(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
							PopupMenu menu = new PopupMenu(context, v);
							menu.getMenuInflater().inflate(R.menu.book_comic_menu, menu.getMenu());
							menu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
								@Override
								public boolean onMenuItemClick(MenuItem menuItem) {
									// TODO Auto-generated method stub
									switch (menuItem.getItemId()) {
									case R.id.menu_download:
										if(onPopupMenuClick != null){
											onPopupMenuClick.onDownloadClicked(store, SearchItem.this);
										}
										break;
									case R.id.menu_add_wishlist:
										if(onPopupMenuClick != null){
											onPopupMenuClick.onAddToWishListClicked(store, SearchItem.this);
										}
										break;
									}
									return true;
								}
							});
							menu.show();
						} else {
							main.java.com.u1aryz.android.lib.newpopupmenu.PopupMenu popupMenuCompat = new main.java.com.u1aryz.android.lib.newpopupmenu.PopupMenu(context);
							popupMenuCompat.add(1, R.string.download);
							popupMenuCompat.add(2, R.string.add_to_wishlist);
							popupMenuCompat.setOnItemSelectedListener(new OnItemSelectedListener() {
								@Override
								public void onItemSelected(main.java.com.u1aryz.android.lib.newpopupmenu.MenuItem menuItem) {
									switch (menuItem.getItemId()) {
									case 1:
										if(onPopupMenuClick != null){
											onPopupMenuClick.onDownloadClicked(store, SearchItem.this);
										}
										break;
									case 2:
										if(onPopupMenuClick != null){
											onPopupMenuClick.onAddToWishListClicked(store, SearchItem.this);
										}
										break;
									}
								}
							});
							popupMenuCompat.show(v);
						}
					}
				});
		} else if(applicationType.equalsIgnoreCase("film")){
			if (price == 0 && price2 == 0) {
				aq.id(holder.price).text(res.getString(R.string.free).toUpperCase(Locale.US)).typeface(robotoMedium).textColor(getColor(store));
				aq.id(holder.priceUnit).invisible();
			} else if(price == 0 && price2 != 0){
				aq.id(holder.price).text(String.valueOf(price2)).typeface(robotoMedium).textColor(purpleColor);
				aq.id(holder.priceUnit).image(R.drawable.tym_purple).visible();
			} else if(price != 0 && price2 == 0){
				aq.id(holder.price).text(String.valueOf(price)).typeface(robotoMedium).textColor(greenColor);
				aq.id(holder.priceUnit).image(R.drawable.tym_green).visible();
			}
			aq.id(holder.btnQuickMenu).visible().clicked(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
						PopupMenu menu = new PopupMenu(context, v);
						menu.getMenuInflater().inflate(R.menu.movie_menu, menu.getMenu());
						menu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
							@Override
							public boolean onMenuItemClick(MenuItem menuItem) {
								// TODO Auto-generated method stub
								switch (menuItem.getItemId()) {
								case R.id.menu_watch:
									if(onPopupMenuClick != null){
										onPopupMenuClick.onWatchClicked(SearchItem.this);
									}
									break;
								case R.id.menu_add_wishlist:
									if(onPopupMenuClick != null){
										onPopupMenuClick.onAddToWishListClicked(store, SearchItem.this);
									}
									break;
								}
								return true;
							}
						});
						menu.show();
					} else {
						main.java.com.u1aryz.android.lib.newpopupmenu.PopupMenu popupMenuCompat = new main.java.com.u1aryz.android.lib.newpopupmenu.PopupMenu(context);
						popupMenuCompat.add(1, R.string.watch);
						popupMenuCompat.add(2, R.string.add_to_wishlist);
						popupMenuCompat.setOnItemSelectedListener(new OnItemSelectedListener() {
							@Override
							public void onItemSelected(main.java.com.u1aryz.android.lib.newpopupmenu.MenuItem menuItem) {
								switch (menuItem.getItemId()) {
								case 1:
									if(onPopupMenuClick != null){
										onPopupMenuClick.onWatchClicked(SearchItem.this);
									}
									break;
								case 2:
									if(onPopupMenuClick != null){
										onPopupMenuClick.onAddToWishListClicked(store, SearchItem.this);
									}
									break;
								}
							}
						});
						popupMenuCompat.show(v);
					}
				}
			});
		}
		return convertView;
	}

	private static class ViewHolder {
		ImageView icon;
		TextView appName;
		TextView author;
		RatingBar rating;
		TextView price;
		ImageView priceUnit;
		ImageButton btnQuickMenu;
		ImageView ribbonGift;
//		TextView commentNum;
	}
	
	public interface OnPopupMenuClick {
		public void onInstallClicked(String store, SearchItem item);
		public void onLaunchClicked(String packageName);
		public void onAddToWishListClicked(String store, SearchItem item);
		public void onDownloadClicked(String store, SearchItem item);
		public void onWatchClicked(SearchItem item);
	}
}
